package com.wg.service;

import com.alibaba.fastjson.JSON;
import com.wg.model.ShowContent;
import com.wg.repository.ContentDocumentRepository;
import com.wg.result.BasePageData;
import com.wg.util.LogUtil;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

/**
 * Created by qts on 2019/1/25.
 */
@Service
public class EsServiceImpl implements EsService {

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
    private ContentDocumentRepository contentDocumentRepository;


    @Override
    public Boolean createContent() {
        return null;
    }

    @Override
    public Boolean save(ShowContent showContent) {
        elasticsearchTemplate.putMapping(ShowContent.class);
        if(showContent != null){
           LogUtil.getDefaultLog().info(JSON.toJSONString(contentDocumentRepository.save(showContent)));
        }
        return true;
    }

    @Override
    public ShowContent getById(Long id) {
        return contentDocumentRepository.findOne(id);
    }

    @Override
    public List<ShowContent> query(String title) {
        List<ShowContent> list = new ArrayList<>();
        SearchQuery searchQuery = new NativeSearchQueryBuilder().
                withQuery(matchQuery("title", title))
                .build();
        Iterable<ShowContent> searchResult = contentDocumentRepository.search(searchQuery);
        searchResult.forEach(list::add);
        return list;
    }

    @Override
    public  BasePageData<ShowContent> page(String title) {
        // 分页参数
        List<ShowContent> list = new ArrayList<>();

        String ids = "1,2";
        String[] schoolIds = ids.split(",");
        BoolQueryBuilder builder = QueryBuilders.boolQuery().
                must(QueryBuilders.termsQuery("id", schoolIds)).
                must(QueryBuilders.matchQuery("title", title));

        TermsQueryBuilder idSearch = QueryBuilders.termsQuery("id", schoolIds);
        FuzzyQueryBuilder titleSearch = QueryBuilders.fuzzyQuery("title", title);
        //builder.must(idSearch);
       // builder.should(titleSearch);
        //Pageable pageable = new PageRequest(0, 10);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(builder)
                //.withPageable(pageable)
                .build();
        Iterable<ShowContent> searchResult = contentDocumentRepository.search(builder);
        searchResult.forEach(list::add);
        BasePageData<ShowContent> basePageData = new BasePageData<>();
        basePageData.setPageNum(0);
        basePageData.setPageSize(10);
        basePageData.setResults(list);

        return basePageData;
    }


}
